<?php

namespace App\Api\Service;

class BaseService
{
    /**
     * 将外部数据修改为列表展示的数据格式
     *
     * @param int $uid
     * @param array $data
     * @return array
     */
    protected function make_exterior_result_use_list_show($uid, $data)
    {
        if (is_array($data)) {
            $data = json_decode(json_encode($data));
        }
        $log_user_browse = new \App\Api\Repositories\Log\LogUserBrowseRepositories();
        $is_read = $log_user_browse->get_is_read('e' . $data->uid, $uid);
        $log_user_collect = new \App\Api\Repositories\Log\LogUserCollectRepositories();
        $is_collect = $log_user_collect->get_is_collect('e' . $data->uid, $uid);
        $image = '';

        $result['id'] = 'e' . $data->uid;
        $result['image'] = $image;
        if (isset($data->avatar)) {
            $result['avatar'] = $data->avatar;
        }
        if (isset($data->nikename)) {
            $result['nikename'] = $data->nikename;
        }

        $intro = "";
        if (isset($data->location->city)) {
            $intro .= $data->location->city;
        }
        if (isset($data->age)) {
            $intro .= " " . $data->age;
        }
        if (isset($data->height)) {
            $intro .= " " . $data->height . 'cm';
        }
        if (isset($data->sex)) {
            $intro .= " " . $data->sex == '1' ? '男' : '女';
        }
        if (isset($data->education)) {
            $intro .= " " . $data->education;
        }
        if (isset($data->school_type)) {
            $intro .= " " . $data->school_type;
        }
        if (isset($data->constellation)) {
            $intro .= " " . $data->constellation;
        }
        if (isset($data->annual_income)) {
            $intro .= " " . $data->annual_income;
        }
        if (isset($data->tags)) {
            $tags = implode(" ", $data->tags);
            $intro .= " " . $tags;
        }
        $result['intro'] = $intro;
        $result['is_read'] = $is_read;
        $result['is_collect'] = $is_collect;
        $result['created_at'] = date("Y-m-d H:i:s", $data->publish_time);
        $result['is_inside_outside'] = 2;
        return $result;
//        return [
//            'id' => 'e' . $data->uid,
//            'avatar' => "",
//            'nickname' => "",
//            'image' => $image,
//            'intro' => implode(' ', array_merge([
//                $data->location->city,
//                $data->age,
//                $data->height . 'cm',
//                $data->sex == '1' ? '男' : '女',
//                $data->education,
//                $data->school_type,
//                $data->constellation,
//                $data->annual_income,
//            ], $data->tags)),
//            'is_read' => $is_read,
//            'is_collect' => $is_collect,
//            'created_at' => date("Y-m-d H:i:s", $data->publish_time)
//        ];
    }

    /**
     * 将内部数据修改为列表展示的数据格式
     *
     * @param int $uid 搜索者的会员id
     * @param array $data 搜索到的信息
     * @return void
     */
    protected function make_interior_result_use_list_show($uid, $data)
    {
        $log_user_browse = new \App\Api\Repositories\Log\LogUserBrowseRepositories();
        $is_read = $log_user_browse->get_is_read('i' . $data['id'], $uid);
        $log_user_collect = new \App\Api\Repositories\Log\LogUserCollectRepositories();
        $is_collect = $log_user_collect->get_is_collect('i' . $data['id'], $uid);
        $image = '';
        foreach (['general_photo', 'life_photo', 'work_photo', 'study_photo', 'travel_photo', 'other_photo'] as $v) {
            if ($data[$v] != '') {
                $image = $data[$v];
                break;
            }
        }
        return [
            'id' => 'i' . $data['id'],
            'avatar' => $data['avatar'],
            'nickname' => $data['nickname'],
            'image' => $image,
            'intro' => implode(' ', array_merge([
                $data['city'],
                $data['age'],
                $data['height'] . 'cm',
                $data['sex'],
                $data['education'],
                $data['college'],
                $data['constellation'],
                $data['income'],
                $data['constellation'],
                $data['constellation'],
            ], explode(',', $data['keyword']))),
            'is_read' => $is_read,
            'is_collect' => $is_collect,
            'created_at' => $data['updated_at'],
            'is_inside_outside' => 1
        ];
    }
}
